<?php 
session_start(); 
$_SESSION['schedule']="visibility: hidden;";
$_SESSION['show']=false;

//include files
require_once('head.php'); 		//head file with javascript and css definitions
require_once('query.php'); 		//file containing the code which is used to query
require_once('dijkstra.php');	//file containing the code for our dijkstra implementation


/* function which draws the schedule containing the computed route between
 * the start and the stop. In here our Dijkstra is called.
 */
function drawSchedule(){
		$_SESSION['schedule']="visibility: visible;";
		$_SESSION['show']=true;
	
		echo '<div id="schedule" style="'.$_SESSION['schedule'].'" >';
		
		$start = utf8_encode($_POST['start']);
		$end = utf8_encode($_POST['end']);
		
		echo "<font size='1'>From ".$_POST['start']." to ".$_POST['end']."</font><br />";

		echo '<table style="border: 2px;"><th>Route</th><th>Stop</th>';
		$coordinates = array(); //this array will contain the coordinates of the stops computed by the dijkstra
		foreach ( startDijkstra($start,$end) as $stop ){ //Call the Dijkstra and iterate over the returned array
			echo "<tr><td><b>".$stop['route']."</b></td> <td>".$stop['label']."</td></tr>";
			array_push($coordinates,array('lat' => $stop['lat'], 'lon' => $stop['lon']));
		}
		echo "</table>";
		echo '<script type="text/javascript">';
		for($i = 0; $i<count($coordinates)-1; $i++){
			echo 'drawPartOfLine('.$coordinates[$i]['lat'].', '.$coordinates[$i]['lon'].', '.$coordinates[$i+1]['lat'].', '.$coordinates[$i+1]['lon'].');';
		}
		echo 'drawLine();</script>';
		echo '</div>';
}
?>
</head>
<body onload="startMap()">

	<!-- Map -->
	<div id="map">
		<!-- Logos + Headline -->
		<div id="headLogos">
			<h1><a class="nolink" href="index.php">LIDEL Bus Application</a></h1>
		</div>
		<!-- Bottom line -->
		<div id="legal">
			Responsible: 
			<a href="http://ifgi.uni-muenster.de/">Institute for Geoinformatics </a> 
			in cooperation with 
			<a href="http://www.stadtwerke-muenster.de/">Stadtwerke Muenster</a>
			<!-- Credits -->
			<span id="credits">Florian Lahn, Jakob Moellers, Gerald Pape, Dennis Wilhelm</span>
		</div>
	</div>
	<!-- Schedule -->
		<?php 
		//if the user has submitted,
		if (isset($_POST['start']) && isset($_POST['end']) && isset($_POST['action'])) {
			drawSchedule(); //draw the schedule
		}
		?>
			
<!-- Search Box -->	
		<form method="post" id="control">
			<input type="hidden" name="action" value="query" />
				<label for="start">Start:</label><br />
				<input class="text_fields" name="start" id="start" type="text" <?php if($_SESSION['show']){ echo 'value="'.$_POST['start'].'"';} ?>><br />
				<label for="end">Destination:</label><br />
				<input class="text_fields" name="end" id="end" type="text" <?php if($_SESSION['show']){ echo 'value="'.$_POST['end'].'"';} ?>><br />
				<input type="submit" style="width: 50%; margin-top:10px;" value="Search"><input type="button" style="width: 50%; margin-top:10px;" value="Clear" onclick="hideSchedule();">
		</form>
		
</body>
</html>

